package com.ts.content.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ts.common.core.utils.Result;
import com.ts.common.security.dto.UserLoginDto;
import com.ts.content.entity.PostEntity;
import com.ts.content.service.PostService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/post")
@Slf4j
public class PostController{

    @Autowired
    private PostService postService;

    @PostMapping("/page")
    public Page<PostEntity> page(@RequestBody Page<PostEntity> page) {
        return postService.page(page);
    }


    @PostMapping
    public Result release() {

        return null;
    }


    /**
     * 根据topicId查询
     *
     * @param topicId
     * @return
     */
    @GetMapping("/postClassList")
    public Result postClassList(@RequestParam Integer topicId) {
        Map<String, Object> map = new HashMap<>();
        map.put("topic_Id", topicId);
        List<PostEntity> list = postService.listByMap(map);
        return new Result().ok(list);
    }

    /**
     * 发布动态
     *
     * @param postEntity
     */
    @PostMapping("/release")
    public Result release(@RequestBody PostEntity postEntity) {
        System.out.println(postEntity);
        boolean save = postService.save(postEntity);
        if (save) {
            return new Result().viewOk(200);
        } else {
            return new Result().error(0);
        }
    }

    /**
     * 获取帖子列表
     *
     * @param page
     * @param topicId  圈子id
     * @param class_id 类名id
     * @return
     */
    @GetMapping("/list")
    public Result list(@RequestParam Integer page, @RequestParam Integer topicId, @RequestParam Integer class_id) {
        System.out.println("page = " + page + ", topic_id = " + topicId + ", class_id = " + class_id);
        Map<String, Object> map = new HashMap<>();
        map.put("topic_id", topicId);
        List<PostEntity> postEntities = postService.listByMap(map);
        return new Result().ok(postEntities);
    }


    /**
     * 查询我的帖子
     *
     * @param page
     * @return
     */
    @GetMapping("/releaseList")
    public Result<PostEntity> releaseList(@RequestParam Integer page, UserLoginDto userLoginDto) {
        Integer userId = userLoginDto.getUserId();
        if (ObjectUtils.isEmpty(userId)) {
            return new Result().error(0, "请先登录");
        }
        Page<PostEntity> postPage = new Page<>();
        postPage.setCurrent(page);
        HashMap<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        QueryWrapper<PostEntity> poQW = new QueryWrapper(map);
        Page<PostEntity> p1 = postService.page(postPage, poQW);
        return new Result().ok(p1);
    }


}